﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Battleships_2
{
    class Ship
    {
        public Vector2 shipPosition = new Vector2(0, 0);
        public Texture2D shipTexture;
        public Vector2 shipCenter;
        public Vector2 direction; // Direction vector of our movement
        public GameTime shipTime;
        public float shipAngle = MathHelper.Pi; 
        public string shipDeb; // Debug string to send some info to be printed on the screen
        public Vector2 mouseLocation;
        public Vector2 mouseDirection;

        private int acceleration = 0; // Acceleration factor. When positive it will move the ship forward. While negative it will move the ship backwards.
        


        public void MouseMovement()
        {

            MouseState currentMouse = Mouse.GetState();
            mouseLocation = new Vector2(currentMouse.X, currentMouse.Y);

            shipAngle = (float)(Math.Atan2(shipPosition.X - mouseLocation.X, shipPosition.Y - mouseLocation.Y) * -1);
        }


        public float ShipRotationLeft() // Rotates the ship left by subtracting from the ship's angle.
        {
            return shipAngle -= 0.01f;
        }

        public float ShipRotationRight() // Rotates the ship right by subtracting from the ship's angle.
        {
            return shipAngle += 0.01f;
        }

        public void UpdatePosition() // Updates the ships position. It will add the direciton vector to the ships current position. Also performs drag to constantly reduce the acceleration.
        {
            shipDeb = acceleration.ToString();

            if (acceleration > 0)
            {
                acceleration -= shipTime.ElapsedGameTime.Milliseconds * 2;
            }
            else if (acceleration < 0)
            {
                acceleration += shipTime.ElapsedGameTime.Milliseconds / 2;
            }

            mouseDirection = mouseLocation - shipPosition;

            if (mouseDirection.Length() > 20)
            {
                if (acceleration < 1000)
                {
                    if (acceleration < 0)
                    {
                        acceleration += shipTime.ElapsedGameTime.Milliseconds / 2;
                    }
                    else if (acceleration >= 0)
                    {
                        acceleration += shipTime.ElapsedGameTime.Milliseconds * 4;
                    }
                }

                direction = mouseDirection;
                direction.Normalize();
            }



            if (acceleration > 0)
            {
                shipPosition += direction * acceleration / 1000;
            }
            else if (acceleration < 0)
            {
                shipPosition += direction * acceleration / 1000;
            }

        }

        public void MoveForward(float speed)
        {
            if (acceleration < 1000)
            {
                if (acceleration < 0)
                {
                    acceleration += shipTime.ElapsedGameTime.Milliseconds / 2;
                }
                else if (acceleration >= 0)
                {
                    acceleration += shipTime.ElapsedGameTime.Milliseconds;
                }
            }

           
            
            direction.X = shipPosition.X + (float)Math.Sin(shipAngle) * speed;
            direction.Y = shipPosition.Y - (float)Math.Cos(shipAngle) * speed;
            

            direction = direction - shipPosition;
        }



        public void MoveBack(float speed2)
        {
            if (acceleration > -250)
            {
                if (acceleration > 0)
                {
                    acceleration -= shipTime.ElapsedGameTime.Milliseconds / 2;
                }
                else if (acceleration <= 0)
                {
                    acceleration -= shipTime.ElapsedGameTime.Milliseconds;
                }
            }

            
            direction.X = shipPosition.X + (float)Math.Sin(shipAngle) * speed2;
            direction.Y = shipPosition.Y - (float)Math.Cos(shipAngle) * speed2;
            
            direction = direction - shipPosition;
        }


        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            shipTexture = theContentManager.Load<Texture2D>(theAssetName);
            shipCenter.X = shipTexture.Width / 2;
            shipCenter.Y = shipTexture.Height / 2;
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(shipTexture, shipPosition, null, Color.Wheat, shipAngle, shipCenter, 1.0f, SpriteEffects.None, 0f);
        }
    }
}
